package com.qimu.saiyou.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qimu.saiyou.common.BaseResponse;
import com.qimu.saiyou.common.ErrorCode;
import com.qimu.saiyou.common.ResultUtils;
import com.qimu.saiyou.exception.BusinessException;
import com.qimu.saiyou.model.dto.compete.CompeteQueryRequest;
import com.qimu.saiyou.model.entity.User;
import com.qimu.saiyou.model.vo.CompeteVo;
import com.qimu.saiyou.service.CompeteService;
import com.qimu.saiyou.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author: FeiXiang  考试，比赛
 * @Date: 2024年02月26日 19:58
 * @Version: 1.0
 * @Description:
 */
@RestController
@RequestMapping("/compete")
@Slf4j
public class CompeteController {
    @Resource
    private CompeteService competeService;
    @Resource
    private UserService userService;

    @PostMapping("/list/page/vo/competitionId")
    public BaseResponse<Page<CompeteVo>> listCompeteVoByCompetitionId(@RequestBody CompeteQueryRequest competeQueryRequest, HttpServletRequest request) {
        if (competeQueryRequest == null || competeQueryRequest.getCompetitionId() == null
                || competeQueryRequest.getCompetitionId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<CompeteVo> competeVoPage = competeService.listCompeteVoByCompetitionId(competeQueryRequest, request);
        return ResultUtils.success(competeVoPage);
    }

    @PostMapping("/list/page/vo/teamId")
    public BaseResponse<Page<CompeteVo>> listCompeteVoByTeamId(@RequestBody CompeteQueryRequest competeQueryRequest, HttpServletRequest request) {
        if (competeQueryRequest == null || competeQueryRequest.getTeamId() == null
                || competeQueryRequest.getTeamId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<CompeteVo> competeVoPage = competeService.listCompeteVoByTeamId(competeQueryRequest, request);
        return ResultUtils.success(competeVoPage);
    }

    @PostMapping("/list/page/vo/id")
    public BaseResponse<CompeteVo> listCompeteVoById(@RequestBody CompeteQueryRequest competeQueryRequest, HttpServletRequest request) {
        if (competeQueryRequest == null || competeQueryRequest.getId() == null || competeQueryRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        CompeteVo competeVoPage = competeService.listCompeteVoById(competeQueryRequest, request);
        return ResultUtils.success(competeVoPage);
    }

    @PostMapping("/list/my/page/vo")
    public BaseResponse<Page<CompeteVo>> listMyCompeteVoPage(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Page<CompeteVo> competeVoPage = competeService.listMyCompeteVoPage(loginUser);
        return ResultUtils.success(competeVoPage);
    }
}
